package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lengmou.entity.Coupons;
import com.lengmou.entity.Orders;
import com.lengmou.entity.OrdersInfo;
import com.lengmou.entity.Users;
import com.lengmou.service.CouponsService;
import com.lengmou.service.OrdersInfoService;
import com.lengmou.service.OrdersService;
import com.lengmou.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrdersController {

    @Resource
    private OrdersService ordersService;
    @Resource
    private OrdersInfoService ordersInfoService;
    @Resource
    private CouponsService couponsService;
    @Resource
    private UsersService usersService;

    @RequestMapping("/info")
    public ModelAndView ordersInfo(int ordersid){
        ModelAndView mav = new ModelAndView();
        Orders ordersById = ordersService.getOrdersById(ordersid);
        if(ordersById!=null){
//            获取订单详情
            List<OrdersInfo> infoByOrdersId = ordersInfoService.getInfoByOrdersId(ordersid);
            mav.addObject("ordersinfo",infoByOrdersId);
            mav.addObject("orders",ordersById);
//            获取优惠券对象
            Coupons couponsByOrdersId = couponsService.getCouponsByOrdersId(ordersid);
            mav.addObject("coupons",couponsByOrdersId);
            mav.setViewName("orderinfo");
        }else{
//            找不到
            mav.setViewName("error");
        }

        return mav;
    }


    @RequestMapping(value = "/allOrders",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAll(int pageNum,int pageSize,Integer id,String createtime,String paytime,Integer state,Integer userid){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("createtime",createtime);
        map.put("paytime",paytime);
        map.put("state",state);
        map.put("userid",userid);
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> all = ordersService.search(map);
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",all);
        json.put("count",ordersService.search(map).size());
        return json.toJSONString();
    }
    @RequestMapping(value = "/setCreatetime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCreatetime(int ordersid,String createtime){
        JSONObject json = new JSONObject();
        Orders ordersById = ordersService.getOrdersById(ordersid);
        if(ordersById!=null){
            ordersById.setCreatetime(createtime);
            int i = ordersService.setOrdersCreateTime(ordersById);
            json.put("state",i);
        }else{
//            找不到订单
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping(value = "/setPaytime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setPaytime(int ordersid,String paytime){
        JSONObject json = new JSONObject();
        Orders ordersById = ordersService.getOrdersById(ordersid);
        if(ordersById!=null){
            ordersById.setPaytime(paytime);
            int i = ordersService.setOrdersPayTime(ordersById);
            json.put("state",i);
        }else{
//            找不到订单
            json.put("state",-1);
        }
        return json.toString();
    }


    @RequestMapping(value = "/updata",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updata(int id,int userid,String remarks){
        JSONObject json = new JSONObject();
        Users userById = usersService.getUserById(userid);
        Orders ordersById = ordersService.getOrdersById(id);
        if(userById!=null){
            if(ordersById!=null){
                ordersById.setUserid(userid);
                ordersById.setRemarks(remarks);
                int i = ordersService.updateOrders(ordersById);
                json.put("state",i);
            }else{
                json.put("state",-2);
            }
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setOrderState",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setOrderState(int id,int state){
        JSONObject json = new JSONObject();
        Orders ordersById = ordersService.getOrdersById(id);
        if(ordersById!=null){
            ordersById.setState(state);
            int i = ordersService.setOrdersState(ordersById);
            json.put("state",1);
        }else{
//            找不到该订单
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setOrderUserid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setOrderUserid(int id,int userid){
        JSONObject json = new JSONObject();
        Orders ordersById = ordersService.getOrdersById(id);
        if(ordersById!=null){
            ordersById.setUserid(userid);
            int i = ordersService.setOrdersUserid(ordersById);
            json.put("state",1);
        }else{
//            找不到该订单
            json.put("state",-1);
        }
        return json.toString();
    }

//    添加订单
    @RequestMapping(value = "/addOrders",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addOrders(Orders orders){
        JSONObject json = new JSONObject();
        int i = ordersService.addOrders(orders);
        json.put("state",i);
        return json.toString();
    }
    @RequestMapping(value = "/jsonOrders",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String jsonOrders() {
        JSONObject json = new JSONObject();
        List<Orders> all = ordersService.getAll();
        json.put("orders",all);
        return json.toString();
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<Orders> orders){
        JSONObject json = new JSONObject();
        if(orders!=null&&orders.size()>0){
            int i = 0;
            for (Orders o:orders) {
                int i1 = ordersInfoService.deleteByOrdersid(o.getId());
                i+=ordersService.deleteOrders(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

}
